package com.ych.DemoSSM.common.util;

import org.apache.commons.lang3.StringUtils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.math.BigInteger;
import java.security.MessageDigest;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

public class SystemUtils {

    public static Map<String, Object> getSuccessMap(Object data) {
        Map<String, Object> ret = new HashMap<String, Object>();
        ret.put("message", "成功");
        ret.put("code", "0");
        ret.put("data", data);

        return ret;
    }

    public static Map<String, Object> getFailMap(String msg) {
        Map<String, Object> ret = new HashMap<String, Object>();
        ret.put("message", msg);
        ret.put("code", "1");

        return ret;
    }

    public static boolean validateGraphVeriCode(String veriCode, HttpSession session) {
        String graphVeriCode = (String) session.getAttribute("graphVeriCode");

        if (StringUtils.isNotBlank(graphVeriCode)) {
            long validateTime = (Long) session.getAttribute("graphVeriCodeValidTime");

            return System.currentTimeMillis() <= validateTime && StringUtils.equalsIgnoreCase(veriCode, graphVeriCode);
        }

        return false;
    }

    public static String getMD5(String str) {
        try {
            // 生成一个MD5加密计算摘要
            MessageDigest md = MessageDigest.getInstance("MD5");
            // 计算md5函数
            md.update(str.getBytes());
            // digest()最后确定返回md5 hash值，返回值为8为字符串。因为md5 hash值是16位的hex值，实际上就是8位的字符
            // BigInteger函数则将8位的字符串转换成16位hex值，用字符串来表示；得到字符串形式的hash值
            return new BigInteger(1, md.digest()).toString(16);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return null;
    }

    /**
     * 从session-userInfo中获取用户id
     *
     * @param session HttpSession
     * @return userID
     */
    public static String getSessionUserID(HttpSession session) {
        if (session == null) {
            return null;
        }
        Map<String, Object> userInfo = (Map<String, Object>) session.getAttribute("userInfo");
        if (userInfo == null) {
            return null;
        }
        return ConverterUtils.toString(userInfo.get("userID"));
    }

    /**
     * 从session-userInfo中获取用户授权类型
     *
     * @param session HttpSession
     * @return 用户授权类型
     */
    public static int getSessionGrantType(HttpSession session) {
        if (session == null) {
            return 1;
        }
        Map<String, Object> userInfo = (Map<String, Object>) session.getAttribute("userInfo");
        if (userInfo == null) {
            return 1;
        }
        return ConverterUtils.toInt(userInfo.get("grantType"));
    }

    public static String getManageUserID(HttpSession session) {
        if (session == null) {
            return null;
        }
        Map<String, Object> userInfo = (Map<String, Object>) session.getAttribute("manageUserInfo");
        if (userInfo == null) {
            return null;
        }
        return ConverterUtils.toString(userInfo.get("userID"));
    }

    public static String getUUID() {
        String uuid = UUID.randomUUID().toString();
        return uuid.replaceAll("-", "");
    }

    /**
     * 从Request对象中获得客户端IP，处理了HTTP代理服务器和Nginx的反向代理截取了ip
     *
     * @param request
     * @return ip
     */
    public static String getRemoteIP(HttpServletRequest request) {
        String ip = request.getHeader("X-Forwarded-For");
        if (StringUtils.isNotEmpty(ip) && !"unKnown".equalsIgnoreCase(ip)) {
            //多次反向代理后会有多个ip值，第一个ip才是真实ip
            int index = ip.indexOf(",");
            if (index != -1) {
                return ip.substring(0, index);
            } else {
                return ip;
            }
        }
        ip = request.getHeader("X-Real-IP");
        if (StringUtils.isNotEmpty(ip) && !"unKnown".equalsIgnoreCase(ip)) {
            return ip;
        }
        return request.getRemoteAddr();
    }

    public static void main(String[] args) {
        System.out.println(getMD5("jpsb88835718**"));
    }

}
